//
//  FDMAlbum.m
//  轮播图封装
//
//  Created by dllo on 16/4/12.
//  Copyright © 2016年 付冬梅. All rights reserved.
//

#import "FDMAlbum.h"
#import "FDMScrollView.h"

@interface FDMAlbum ()<UIScrollViewDelegate>

@property (nonatomic, strong) FDMScrollView *scrollView;/**< 轮播图的底层滚动视图 */

@property (nonatomic, strong) UIPageControl *pageControl;/**< 页标 */

@property (nonatomic, strong) NSArray *picArr;/**< 图片数组 */

@property (nonatomic, assign) NSTimeInterval time;

@end
@implementation FDMAlbum

#pragma mark - Creating And Init
- (instancetype)initWithFrame:(CGRect)frame withImages:(NSArray<UIImage *> *)images andTime:(NSTimeInterval)time {
    self = [super initWithFrame:frame];
    if (self) {
        self.picArr = [NSMutableArray array];
        self.picArr = images;
        [self configWithFrame:frame withImages:images];
        [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(timeAction:) userInfo:nil repeats:YES];
    }
    return self;
}

#pragma mark - config(布局)
- (void)configWithFrame:(CGRect)frame withImages:(NSArray *)images {
    
    // 创建滚动视图对象（参数原点不是（0，0）用bounds属性代替）
    self.scrollView = [[FDMScrollView alloc] initWithFrame:self.bounds withImages:images delegate:self];
    [self addSubview:self.scrollView];
    
    // 指定代理人
    self.scrollView.delegate = self;
    
    // 创建pageControl对象
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, frame.size.height - 40, frame.size.width, 40)];
    [self addSubview:self.pageControl];
    
    // 页数
    self.pageControl.numberOfPages = images.count;
    
    // 添加事件
    [self.pageControl addTarget:self action:@selector(pageControlAction:) forControlEvents:UIControlEventValueChanged];

}

- (void)pageControlAction:(UIPageControl *)page {
    
    // 设置页标与滚动视图的联动
    [self.scrollView setContentOffset:CGPointMake(self.bounds.size.width * (page.currentPage + 1), 0) animated:YES];
}

- (void)timeAction:(NSTimer *)time {
    [self scrollViewDidEndDecelerating:self.scrollView];
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.bounds.size.width + self.scrollView.contentOffset.x, 0) animated:YES];
    if (self.scrollView.contentOffset.x / self.bounds.size.width == self.picArr.count) {
        self.pageControl.currentPage = 0;
    } else {
        self.pageControl.currentPage = self.scrollView.contentOffset.x / self.bounds.size.width;
    }
}


#pragma mark - ScrollViewDelegate
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 页标跟随滚动视图变化
    self.pageControl.currentPage = scrollView.contentOffset.x / self.bounds.size.width;
    
    // 判断滑动的滚动视图是大滚动视图时
    if (scrollView == self.scrollView) {
        for (UIScrollView *scrollOfSmall in self.scrollView.subviews) {
            if ([scrollOfSmall.class isSubclassOfClass:[UIScrollView class]]) {
                
                scrollOfSmall.zoomScale = 1;
                
            }
        }
    }
    
    if (self.scrollView.contentOffset.x == 0) {
        [self.scrollView setContentOffset:CGPointMake(self.picArr.count * self.scrollView.bounds.size.width, 0)  animated:NO];
    }
    if (self.scrollView.contentOffset.x == (self.picArr.count + 1) * self.scrollView.bounds.size.width) {
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.bounds.size.width, 0) animated:NO];
    }
    

    
}

/** 返回需要缩放的视图 */
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    
    // 判断是小的scrollView
    if (scrollView != self.scrollView) {
        return [scrollView.subviews firstObject];
    } else {
        return nil;
    }
}


@end
